package xyz.penghaonan.p2plib.helper.connect;

import android.net.wifi.p2p.WifiP2pInfo;

import com.penghaonan.appframework.AppDelegate;
import com.penghaonan.appframework.utils.WeakObservableList;

import xyz.penghaonan.p2plib.P2PManager;
import xyz.penghaonan.p2plib.helper.message.MsgSender;
import xyz.penghaonan.p2plib.helper.p2phelper.WifiP2PListenerAdapter;
import xyz.penghaonan.p2plib.helper.sockethelper.ServerSocketHelper;
import xyz.penghaonan.p2plib.helper.sockethelper.SocketBase;
import xyz.penghaonan.p2plib.job.base.JobListenerAdapter;
import xyz.penghaonan.p2plib.job.jobs.StartServerJob;
import xyz.penghaonan.p2plib.job.jobs.StopServerJob;

public class ServerConnectHelper {
    public static final int STATE_OFF = 1;
    public static final int STATE_STARTING = 2;
    public static final int STATE_WAITING = 3;
    public static final int STATE_CONNECT = 4;
    public static final int STATE_FAILED = 5;

    private int state = STATE_OFF;

    private static final WeakObservableList<ServerConnectHelper.OnConnectStateChangedListener> connectStateChangedListenerList = new WeakObservableList<>();

    public interface OnConnectStateChangedListener {
        void onConnectStateChanged(int state);

        void onConnectProgress(float progress, String msg);

        void onConnectFinished(boolean success, String msg);
    }

    public void addOnConnectStateChangedListener(ServerConnectHelper.OnConnectStateChangedListener listener) {
        connectStateChangedListenerList.addListener(listener);
    }

    public void removeOnConnectStateChangedListener(ServerConnectHelper.OnConnectStateChangedListener listener) {
        connectStateChangedListenerList.removeListener(listener);
    }

    public void onConnect() {
        setState(STATE_CONNECT);
    }

    public void onDisconnect() {
        setState(STATE_OFF);
    }

    public boolean isOn() {
        int state = getState();
        return state != STATE_FAILED && state != STATE_OFF;
    }

    private void setState(final int state) {
        if (this.state == state) {
            return;
        }
        this.state = state;
        AppDelegate.post(new Runnable() {
            @Override
            public void run() {
                connectStateChangedListenerList.iterate(new WeakObservableList.OnIterator<ServerConnectHelper.OnConnectStateChangedListener>() {
                    @Override
                    protected boolean onIterator(ServerConnectHelper.OnConnectStateChangedListener item) {
                        item.onConnectStateChanged(state);
                        return false;
                    }
                });
            }
        });
    }

    public int getState() {
        return state;
    }

    public String getStateDesc() {
        switch (state) {
            case ServerConnectHelper.STATE_CONNECT:
                return "已连接";
            case ServerConnectHelper.STATE_OFF:
                return ("已关闭");
            case ServerConnectHelper.STATE_STARTING:
                return ("正在开启");
            case ServerConnectHelper.STATE_WAITING:
                return ("等待车机连接");
            case ServerConnectHelper.STATE_FAILED:
                return ("开启失败");
        }
        return "";
    }

    public void connect() {
        if (getState() == STATE_STARTING || getState() == STATE_WAITING || getState() == STATE_CONNECT) {
            return;
        }
        MsgSender.initDevice(true);
        setState(STATE_STARTING);
        StartServerJob startClientJob = new StartServerJob();
        startClientJob.addListener(new JobListenerAdapter() {
            @Override
            public void onJobFinish(boolean success, String msg) {
                super.onJobFinish(success, msg);
                if (success) {
                    setState(STATE_WAITING);
                    P2PManager.getP2PHelper().addListener(new WifiP2PListenerAdapter() {
                        @Override
                        public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
                            super.onConnectionInfoAvailable(wifiP2pInfo);
                        }
                    });
                } else {
                    setState(STATE_FAILED);
                }
            }
        });
        startClientJob.start();
    }

    public void disconnect() {
        StopServerJob stopServerJob = new StopServerJob();
        stopServerJob.addListener(new JobListenerAdapter() {
            @Override
            public void onJobFinish(boolean success, String msg) {
                super.onJobFinish(success, msg);
                setState(STATE_OFF);
            }
        });
        stopServerJob.start();
    }
}
